<template lang="pug">
.article
  .article-header
    .article-title 草稿箱
    .article-search
      el-input(prefix-icon="el-icon-search" v-model="searchContent" clearable placeholder="输入标题/关键词" @input="changeSearch")
  .article-content(v-if="list.length")
    .article-item(v-for="item in list" @click.stop="seeDetail(item)")
      .article-item-bd
        .article-item-cover(:style="`background-image: url(${item.coverImageUrl})`")
          .article-item-mask
            .iconfont.el-icon-delete(@click.stop="delDraft(item)")
        .article-item-detail
          .article-item-title {{item.title}}
          .article-item-time(v-if="item.updateTime") 更新于{{new Date(item.updateTime).format('yyyy-MM-dd hh:mm')}}
    .article-item
      .article-upload
        el-popover(
          popper-class="image-popover"
          width="120"
          trigger="click"
        )
          router-link.image-item(tag="a" target="_blank" :to="{ name: 'editArticle' }") 发布图文文章
          router-link.image-item(tag="a" target="_blank" :to="{ name: 'editVideo' }") 发布视频文章
          //- .image-item(@click="selectUpload") 发布图文文章
          //- .image-item(@click="selectImage") 发布视频文章
          .article-upload-btn(slot="reference")
            .iconfont.el-icon-plus
            .article-upload-btn-text 新的创作
  .article-empty(v-else)
      img(src="../../static/images/empty_.png")
      .article-empty-txt 暂无数据...
  .article-footer(v-if="list.length")
    el-pagination(
      background
      layout="prev, pager, next, jumper"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="currentChange"
    )
</template>

<script>
import { getArticleList, postDelArticle } from 'api'
export default {
  name: 'teacherDraft',
  data () {
    return {
      searchContent: '',
      list: [],
      total: 0,
      currentPage: 1,
      pageSize: 20
    }
  },
  methods: {
    changeSearch () {
      this.currentPage = 1
      this.getList()
    },
    currentChange (page) {
      this.currentPage = page
      this.getList()
    },
    seeDetail (item) {
      let name = item.type === 2 ? 'editVideo' : 'editArticle'
      let routeUrl = this.$router.resolve({
        name: name,
        params: { id: item.articleId }
      })
      window.open(routeUrl.href, '_blank')
    },
    delDraft (item) {
      this.$confirm('是否删除该文章草稿？', '提示').then(async () => {
        try {
          this.loading = true
          await this.$axios.post(postDelArticle, { articleId: item.articleId })
          this.$message.success('删除成功！')
          this.getList()
          this.loading = false
        } catch (err) {
          this.loading = false
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    async getList () {
      try {
        this.loading = true
        let query = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          draft: true
        }
        if (this.searchContent) query.searchContent = this.searchContent
        const { ResultSet } = await this.$axios.post(getArticleList, { ...query })
        this.list = ResultSet.userData.data || []
        this.loading = false
      } catch (err) {
        this.loading = false
        this.$message.error(err.message || err.ResultSet.errors)
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getList()
    })
  }
}
</script>

<style lang="scss">
.image-popover {
  &.el-popover {
    padding: 0 !important;
  }
}
</style>

<style lang="scss" scoped>
.article {
  background: #FAFAFE;
  padding: 0 32px;
}
.article-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 25px;
  .article-title {
    font-size: 16px;
    line-height: 22px;
    color: #363636;
  }
}
.article-content {
  padding: 25px 0;
  margin-top: 15px;
  display: flex;
  flex-wrap: wrap;
  background: #fff;
}
.article-item {
  margin-bottom: 30px;
  width: 25%;
  display: flex;
  align-items: center;
  flex-direction: column;
}
.article-item-bd {
  width: 320px;
  overflow: hidden;
}
.article-item-cover {
  position: relative;
  width: 100%;
  height: 210px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  &:hover {
    .article-item-mask {
      display: block;
    }
  }
}
.article-item-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, .5);
  display: none;
  .iconfont {
    position: absolute;
    right: 10px;
    top: 10px;
    width: 32px;
    height: 32px;
    border: 1px solid #DCDFE6;
    background: #fff;
    border-radius: 50%;
    font-size: 16px;
    color: #999;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
}
.article-item-detail {
  padding-bottom: 15px;
}
.article-item-title {
  padding: 0 9px;
  margin-top: 10px;
  font-size: 16px;
  line-height: 16px;
  color: #363636;
  font-weight: 700;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.article-item-time {
  padding: 0 9px;
  margin-top: 10px;
  font-size: 14px;
  line-height: 14px;
  color: #999;
}
.article-upload {
  width: 320px;
  height: 210px;
  background: #FFFFFF;
  border: 1px solid #999999;
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  cursor: pointer;
}
.article-upload-btn {
  text-align: center;
}
.el-icon-plus {
  font-size: 36px;
  color: #999;
}
.article-upload-btn-text {
  margin-top: 2px;
  font-size: 16px;
  line-height: 22px;
  color: #999;
}
.image-item {
  display: block;
  text-align: center;
  padding: 10px 20px;
  cursor: pointer;
}
.article-footer {
  padding: 20px 0;
  text-align: right;
}
</style>